package com.zksk.admin.controller.portal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zksk.admin.api.RemoteFileService;
import com.zksk.admin.api.domain.SysFile;
import com.zksk.admin.service.portal.CarouselService;
import com.zksk.common.core.constant.PortalConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.Carousel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 轮播图类 信息操作处理
 *
 * @author zksk
 */
@Slf4j
@RestController
@RequestMapping("/portal/banner")
@Api(value = "CarouselController", tags = "平台轮播图相关接口")
public class CarouselController extends BaseController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private RemoteFileService fileService;

    @Log(title = "默认门户轮播图查询", businessType = BusinessType.SELECT)
    @ApiOperation(value = "默认门户轮播图查询")
    @GetMapping("/default")
    public R<List<Carousel>> defaultList(){
        // 现阶段暂与平台模块无联系 默认展示至多六张轮播图
        List<Carousel> list = carouselService.list(new QueryWrapper<Carousel>()
                .eq(Carousel.STATUS, PortalConstants.CAROUSEL_STATUS_0)
                .orderByDesc(Carousel.SORT)
                .last("limit 6"));

        return R.ok(list,"返回轮播图成功");
    }

    /**
     * 轮播图管理
     * @param file 文件
     * @param carousel 轮播图实体
     * @return
     */
    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "保存轮播图")
    public R<Carousel> add(@Validated(Insert.class) Carousel carousel,
                           @RequestPart("file") MultipartFile file) {
        String remoteFileNme = "";
        R<SysFile> upload = fileService.upload(file);
        if (upload.getCode() == R.SUCCESS) {
            remoteFileNme = upload.getData().getName();
        }else{
            return R.fail("上传轮播图失败");
        }
        carousel.setUrl(remoteFileNme);
        carousel.setStatus(PortalConstants.CAROUSEL_STATUS_0); // 默认为显示状态
        carousel.setCreateBy(SecurityUtils.getUsername());

        carouselService.save(carousel);
        return R.ok(carousel,"保存成功");
    }

    /**
     * 批量删除轮播图
     * @param ids 轮播图id
     * @return
     */
    @Log(title = "轮播图管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除轮播图(逻辑删除)")
    public R<String> remove(@PathVariable List<Long> ids) {
        carouselService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }

}
